package com.woniu108.evaluate.controller;


import cn.hutool.core.bean.BeanUtil;
import com.woniu108.carService.commons.log.AdminHistoryLog;
import com.woniu108.carService.commos.model.Result;
import com.woniu108.carService.commos.util.JWTUtil;
import com.woniu108.carService.commos.util.RedisUtil;
import com.woniu108.evaluate.dto.ProductReviewDto;
import com.woniu108.evaluate.dto.StoreReviewDto;
import com.woniu108.evaluate.form.ProductReviewReplyForm;
import com.woniu108.evaluate.form.StoreReviewPublishForm;
import com.woniu108.evaluate.form.StoreReviewReplyForm;
import com.woniu108.evaluate.model.StoreReview;
import com.woniu108.evaluate.param.ProductReviewReplyParam;
import com.woniu108.evaluate.param.StoreReviewPublishParam;
import com.woniu108.evaluate.param.StoreReviewReplyParam;
import com.woniu108.evaluate.service.StoreReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.reflections.Store;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 门店评价表 前端控制器
 * </p>
 *
 * @author yanxi
 * @since 2024-05-23
 */
@RestController
@RequestMapping("/storeReview")
@Api(tags = "门店评价接口")
public class StoreReviewController {
    @Resource
    private StoreReviewService storeReviewService;
    @Resource
    private RedisUtil redisUtil;

    /**
     * token获取用户id
     * @param token 会话跟踪
     * @return 用户id
     */
    public String getUserIdByToken(String token){
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseUserToken(refreshToken).get("userId").toString();
    }

    /**
     * token中获取管理员id
     * @param token
     * @return
     */
    public String getAdminId(String token){
        String refreshToken=redisUtil.getString(token);
        String adminId= JWTUtil.parseAdminToken(refreshToken).get("adminId").toString();
        return adminId;
    }

    /**
     * token获取门店管理员id
     * @param token adminId
     * @return adminId
     */
    public String getStoreAdminIdByToken(String token){
        String refreshToken = redisUtil.getString(token);
        return JWTUtil.parseStoreAdminToken(refreshToken).get("storeAdminId").toString();
    }

    /**
     * 向门店发布评价(用户)
     * @param form
     * @param token 用户id
     * @return
     */
    @PostMapping("/publishReviewToStore")
    @ApiOperation("向门店发布评价(用户)")
    public Result<Boolean> publishReviewToStore(@RequestBody StoreReviewPublishForm form, @RequestBody String token){
        String userId = getUserIdByToken(token);

        StoreReviewPublishParam param = BeanUtil.toBean(form, StoreReviewPublishParam.class);
        param.setUserId(userId);
        Boolean aBoolean=storeReviewService.publishReview(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("发布评价成功");
    }

    /**
     * 门店管理员回复评论
     * @param form 评价表id，回复正文
     * @param token
     * @return 真假值
     */
    @PostMapping("/replyByReviewToStore.st")
    @ApiOperation("门店管理员回复评论")
    public Result<Boolean> replyByReview(@RequestBody StoreReviewReplyForm form, @RequestHeader String token){
        String storeAdminId = getStoreAdminIdByToken(token);

        StoreReviewReplyParam param = BeanUtil.toBean(form, StoreReviewReplyParam.class);
        param.setStoreAdminId(storeAdminId);
        Boolean aBoolean=storeReviewService.replyByReview(param);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("回复成功");
    }

    /**
     * 查询某个门店的所有评价
     * @param storeId 门店id
     * @return Dto集合 门店所有评价
     */
    @GetMapping("/queryStoreReviewByStoreId/{storeId}.c")
    @ApiOperation("查询某个门店的所有评价")
    public Result<List<StoreReviewDto>> queryStoreReviewByStoreId(@PathVariable("storeId")@ApiParam("门店id") Integer storeId){
        List<StoreReviewDto> storeReviewDtos=storeReviewService.selectProductReviewByPItemId(storeId);
        return Result.newListInstance(StoreReviewDto.class).setData(storeReviewDtos);
    }

    /**
     * 删除用户自己的评价
     * @param proReviewId
     * @param token
     * @return
     */
    @GetMapping("/delReviewByIdToUser/{proReviewId}")
    @ApiOperation("删除用户自己的评论")
    public Result<Boolean> delReviewByIdToUser(@PathVariable("proReviewId")@ApiParam("评论id") Integer proReviewId,@RequestHeader String token){
        String userId = getUserIdByToken(token);

        Boolean aBoolean=storeReviewService.delReviewByIdToUser(proReviewId,userId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除成功");
    }

    /**
     * 删除评论(管理员)
     * @param proReviewId
     * @return
     */
    @GetMapping("/delReviewById/{proReviewId}.api")
    @ApiOperation("删除评论(管理员)")
    @AdminHistoryLog
    public Result<Boolean> delReviewById(@PathVariable("proReviewId")@ApiParam("评论id") Integer proReviewId){
        Boolean aBoolean=storeReviewService.delReviewById(proReviewId);
        return Result.getResult(Boolean.class).setData(aBoolean).setMsg("删除成功");
    }
}

